<?php
namespace app\admin\controller;

use hg\apidoc\annotation as Apidoc;
use app\common\lib\Show;
use think\facade\Filesystem;

/**
 * @Apidoc\Title("文件上传")
 * @Apidoc\Group("Upload")
 * @Apidoc\Sort("4")
 */

class Upload extends AdminBaseController
{
  /**
   * @Apidoc\Title("上传单张图片")
   * @Apidoc\Desc("上传单张图片接口")
   * @Apidoc\Author("ckmmd")
   * @Apidoc\Url("/admin/upload/pic")
   * @Apidoc\Method("POST")
   * @Apidoc\Tag("文件上传")
   * @Apidoc\Param("pic", type="file",require=true, desc="图片对象" )
   * @Apidoc\Returned("data", type="string", desc="图片地址")
   */
  public function pic()
  {
    if (!$this->request->isPost()){
      return Show::error('非法请求！');
    }
    $img = $this->request->file('pic');
    $img_url = Filesystem::disk('upload')->putFile('pic', $img);
    if (!$img_url){
        return Show::error('上传失败', 501);
    }
    $result = '/upload/'.$img_url;
    return Show::success($result, '上传成功');
  }
}
